package com.ssyt.examinationsystem.controller;

import com.ssyt.examinationsystem.entity.RuleInfo;
import com.ssyt.examinationsystem.service.IRuleInfoService;
import com.ssyt.examinationsystem.service.impl.RuleInfoServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Servlet implementation class GroupPaperController
 */
public class GroupPaperController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public GroupPaperController() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     * response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     * response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 调用规则表的业务对象
        IRuleInfoService ruleService = new RuleInfoServiceImpl();
        // 获取输出流对象
        PrintWriter out = response.getWriter();
        // 获取地址栏上的flag值
        String flag = request.getParameter("flag");
        if ("hand".equals(flag)) {
            hand(request, response);
        } else if ("auto".equals(flag)) {
            auto(request, response);
        } else if ("handRule".equals(flag)) {
            handRule(ruleService, request, response, out);
        } else if ("autoRule".equals(flag)) {
            autoRule(ruleService, request, response, out);
        }

        out.flush();
        out.close();
    }

    /**
     * 自动组卷
     *
     * @param choiceService
     * @param request
     * @param response
     * @param out
     */
    private void autoRule(IRuleInfoService ruleService, HttpServletRequest request, HttpServletResponse response,
                          PrintWriter out) {
        String totalScore = request.getParameter("totalScore");
        String scNumber = request.getParameter("singleNumber");
        String scScore = request.getParameter("scScore");
        String mulNumber = request.getParameter("multipleNumber");
        String mulScore = request.getParameter("mulScore");
        String bkNumber = request.getParameter("blankNumber");
        String bkScore = request.getParameter("blankScore");
        String jdNumber = request.getParameter("judgeNumber");
        String jdScore = request.getParameter("judgeScore");
        String subNumber = request.getParameter("subjectiveNumber");
        String subScore = request.getParameter("subjectiveScore");

        // 创建随机组卷的规则对象
        RuleInfo rule = new RuleInfo(Integer.valueOf(scNumber), Float.valueOf(scScore), Integer.valueOf(mulNumber),
                Float.valueOf(mulScore), Integer.valueOf(bkNumber), Float.valueOf(bkScore), Integer.valueOf(jdNumber),
                Float.valueOf(jdScore), Integer.valueOf(subNumber), Float.valueOf(subScore), Float.valueOf(totalScore));
        // 调用自动组卷的业务方法
        int row = ruleService.autoPacking(rule);

        out.print(row);
    }

    /**
     * 手动组卷的方法
     *
     * @param choiceService
     * @param request
     * @param response
     * @param out
     */
    private void handRule(IRuleInfoService ruleService, HttpServletRequest request, HttpServletResponse response,
                          PrintWriter out) {
        String totalScore = request.getParameter("totalScore");
        String scIds = request.getParameter("single");
        String scScore = request.getParameter("scScore");
        String mulIds = request.getParameter("multiple");
        String mulScore = request.getParameter("mulScore");
        String bkIds = request.getParameter("blank");
        String bkScore = request.getParameter("blankScore");
        String jdIds = request.getParameter("judge");
        String jdScore = request.getParameter("judgeScore");
        String subIds = request.getParameter("subjective");
        String subScore = request.getParameter("subjectiveScore");

        // 创建一个RuleInfo对象
        RuleInfo rule = new RuleInfo(Float.valueOf(scScore), scIds, Float.valueOf(mulScore), mulIds,
                Float.valueOf(bkScore), bkIds, Float.valueOf(jdScore), jdIds, Float.valueOf(subScore), subIds,
                Float.valueOf(totalScore));
        // 调用手动组卷的业务方法
        int row = ruleService.handPacking(rule);

        out.print(row);

    }

    /**
     * 转发到自动选题页面
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void auto(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 转发到自动选题显示页面
        request.getRequestDispatcher("WEB-INF/views/group/autoList.jsp").forward(request, response);
    }

    /**
     * 转发到手动选题页面
     *
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    private void hand(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 转发到手动选题显示页面
        request.getRequestDispatcher("WEB-INF/views/group/handList.jsp").forward(request, response);
    }

}
